<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft>
      <q-btn color="primary" icon="add" unelevated @click="handleCreate">
        {{ $t('Add') }}
      </q-btn>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickCardTips') }}</q-tooltip>
      </q-icon>
    </template>
    <template v-slot:menu="{ row }">
      <q-btn class="col" color="primary" outline unelevated @click="handleBindStation(row)">
        {{ $t('MapCore.BindStation') }}
      </q-btn>
      <template v-if="row.sysInner !== '1'">
        <q-btn color="primary" icon="mdi-square-edit-outline" outline unelevated @click="handleUpdate(row)">
          {{ $t('Edit') }}
        </q-btn>
        <q-btn color="primary" icon="delete" outline unelevated @click="handleDelete(row)">
          {{ $t('Del') }}
        </q-btn>
      </template>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="formInfo.id ? $t('Edit') : $t('Add')" :width="500" btnAlign="center"
           position="right" @beforeClose="beforeClose" @confirm="submitForm">
    <qx-form ref="FormRef" v-model="formInfo" :option="tableOption"></qx-form>
  </q-modal>
  <qx-page v-model="pageVisible" :header="header" :menuBtn="false" fullHeight>
    <div class="full-height row no-wrap items-start justify-between q-gutter-x-md q-pb-md">
      <AreaBindStation v-model="pageVisible" v-model:isRefresh="isRefresh" :isBinding="false"
                       :rowInfo="formInfo"></AreaBindStation>
      <AreaBindStation v-model="pageVisible" v-model:isRefresh="isRefresh" :rowInfo="formInfo"
                       isBinding></AreaBindStation>
    </div>
  </qx-page>
  <q-modal v-model="infoVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="rowInfo" cols="2" headerWidth="100"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n } from 'boot/i18n.js'
import { deleteAPI, postAPI, putAPI } from 'src/api/index.js'
import { getRemoteData, setRemoteData } from 'src/enum/ajaxRemoteData.js'
import { enableOrDisable, yesOrNo } from 'src/enum/index.js'
import AreaBindStation from 'src/views/RCSCore/AreaBindStation.vue'
import { computed, onMounted, ref } from 'vue'
import { createDialog, deepClone } from 'src/utils/utils.js'

const maps = ref([])
const header = ref('')
const rowInfo = ref({})
const formInfo = ref({})
const FormRef = ref(null)
const searchForm = ref({})
const isRefresh = ref(false)
const infoVisible = ref(false)
const pageVisible = ref(false)
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/yfagv/api/mapArea/list' })
getRemoteData('agv_map', '/yfagv/api/agvMap/findList').then(data => (maps.value = data))
const tableOption = computed(() => {
  return {
    grid: true, span: 22, export: true, labelWidth: 100, searchSpan: 4, headerWidth: 60,
    searchLabelWidth: 60,
    column: [{
      label: `${i18n.t('MapCore.AreaCode')}`, field: 'areaCode', minWidth: 100, required: true, topLeft: true
    }, {
      label: `${i18n.t('MapCore.AreaName')}`, field: 'areaName', minWidth: 100, required: true
    }, {
      label: `${i18n.t('MapCore.RelatedMap')}`, field: 'mapGroundCode', type: 'select', minWidth: 100, required: true,
      props: { label: 'mapName', value: 'mapGroundCode' }, options: maps.value,
      hint: formInfo.value.id ? `${i18n.t('MapCore.UpdateRelatedMapTips')}` : ''
    }, {
      label: `${i18n.t('BuiltIn')}`, field: 'sysInner', search: false, display: false, options: yesOrNo.value,
      useTag: true, minWidth: 80
    }, {
      label: `${i18n.t('IsEnable')}`, field: 'isEnable', type: 'radio', searchType: 'select', value: '1',
      options: enableOrDisable.value, useTag: true, topRight: true
    }, {
      label: `${i18n.t('CreationTime')}`, field: 'createdTime', display: false, minWidth: 80, search: false
    }, {
      label: `${i18n.t('Remarks')}`, field: 'remark', type: 'textarea', minWidth: 100, search: false, hide: true
    }]
  }
})

const handleCreate = () => {
  modalVisible.value = true
}
const rowDblClick = row => {
  rowInfo.value = row
  infoVisible.value = true
}
const handleBindStation = (row = {}) => {
  formInfo.value = row
  header.value = `${i18n.t('MapCore.BindStation')}(${row.areaName}--${row.mapName || row.mapGroundCode})`
  pageVisible.value = true
}
const handleUpdate = row => {
  formInfo.value = deepClone(row)
  modalVisible.value = true
}
const handleDelete = row => {
  createDialog({ msg: `${i18n.t('DelTips')}` }, () => {
    deleteAPI(`/yfagv/api/mapArea/${row.id}`, {}, () => startSearch('clear'))
  }, () => {})
}
const formatSearchForm = () => ({ ...searchForm.value })
const startSearch = (action, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
  action === 'clear' && setRemoteData('traffic_control_area')
}
const beforeClose = () => {
  if (FormRef.value) FormRef.value.onReset()
  pageVisible.value = false
  modalVisible.value = false
}
const submitForm = mDone => {
  FormRef.value.validate((form, done) => {
    const API = form.id ? putAPI : postAPI
    API('/yfagv/api/mapArea', form, () => {
      startSearch('clear')
      done()
      mDone && mDone()
    }, () => {
      done()
      mDone && mDone(false)
    })
  }, () => mDone && mDone(false))
}
onMounted(startSearch)
</script>